package com.jp.juc.tools;

import java.util.concurrent.Semaphore;

public class UseSemaphore {
    public static void main(String[] args) {
        Semaphore semaphore = new Semaphore(20, true);
        for (int i = 0; i < 50; i++) {
            new Thread(() -> {
                if (semaphore.tryAcquire()) {
                    try {
                        Thread.sleep(200);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + "-------" + "查询成功");
                    semaphore.release();
                } else {
                    System.out.println(Thread.currentThread().getName() + "-------" + "查询人数过多，请稍后重试！");
                }
            }).start();
        }
    }
}
